/*
 *	BCE console mode
 *
 *	Copyright (C) 2011 XiaoJSoft Studio. All Rights Reserved.
 *	Copyright (C) Ji WenCong <whs_jwc@163.com>
 *
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../include/console/utils/atom.h"
#include "../include/console/utils/molecule.h"
#include "../include/console/cs_main.h"
#include "../include/console/keybio.h"
#include "../include/kernel.h"
#include "../localization/local.h"
#include "../include/strext.h"

/*
 *	void csm_start(int *silent, int *full);
 *
 *	To start the console mode
 *
 */
void csm_start(int *silent, int *full)
{
	int csexit;
	char *cssubin;
	char *csin;
	char *ptr;
	char *ret;
	int enumElement;

	if (silent == NULL || full == NULL) {
		printf(localization_table[20]);
		return;
	}

	csexit = 0;

	while(csexit != 1) {
		printf(localization_table[21]);

		/*  Gets the user-input command  */
		csin = inControl();

		if (csin == NULL) {
			printf(localization_table[22]);
			return;
		}

		for (ptr = csin; *ptr != '\0'; ptr++) {
			if (*ptr == '[' || *ptr == '{') {
				*ptr = '(';
			} else if (*ptr == ']' || *ptr == '}') {
				*ptr = ')';
			}
		}

		if (strlen(csin) <= 0) {
			goto csm_next;
		}

		if (strcmp(csin, localization_table[23]) == 0) {
			free(csin);
			break;
		}

		if (strcmp(csin, localization_table[24]) == 0) {
			if (*silent == 1) {
				printf(localization_table[27]);
			} else {
				printf(localization_table[28]);
			}
			goto csm_next;
		}

		if (strcmp(csin, localization_table[25]) == 0) {
			*silent = 1;
			goto csm_next;
		}

		if (strcmp(csin, localization_table[26]) == 0) {
			*silent = 0;
			goto csm_next;
		}

		if (strcmp(csin, localization_table[29]) == 0) {
			if (*full == 1) {
				printf(localization_table[27]);
			} else {
				printf(localization_table[28]);
			}
			goto csm_next;
		}

		if (strcmp(csin, localization_table[30]) == 0) {
			*full = 1;
			goto csm_next;
		}

		if (strcmp(csin, localization_table[31]) == 0) {
			*full = 0;
			goto csm_next;
		}

		if (strcmp(csin, localization_table[38]) == 0) {
			printf(localization_table[39]);
			goto csm_next;
		}

		if (strcmp(csin, localization_table[40]) == 0) {
			while(!0) {
				printf(localization_table[41]);

				cssubin = inControl();

				for (ptr = cssubin; *ptr != '\0'; ptr++) {
					if (*ptr == '[' || *ptr == '{') {
						*ptr = '(';
					} else if (*ptr == ']' || *ptr == '}') {
						*ptr = ')';
					}
				}

				if (cssubin == NULL) {
					printf(localization_table[42]);
					break;
				}

				if (strlen(cssubin) == 0) {
					free(cssubin);
					continue;
				}

				if (strcmp(cssubin, localization_table[23]) == 0) {
					free(cssubin);
					break;
				}

				analysisMolecule(cssubin);

				free(cssubin);
			}
			goto csm_next;
		}

#ifndef CROSS_COMPILER_WIN32
		if (strcmp(csin, localization_table[43]) == 0) {
			listElements(1, chemical_ecount);
			goto csm_next;
		}
#endif

		if (strcmp(csin, localization_table[44]) == 0) {
			while(!0) {
				printf(localization_table[45]);

				cssubin = inControl();

				if (cssubin == NULL) {
					printf(localization_table[42]);
					break;
				}

				for (ptr = cssubin; *ptr != '\0'; ptr++) {
					if (*ptr == '[' || *ptr == '{') {
						*ptr = '(';
					} else if (*ptr == ']' || *ptr == '}') {
						*ptr = ')';
					}
				}

				if (strlen(cssubin) == 0) {
					free(cssubin);
					continue;
				}

				if (strcmp(cssubin, localization_table[23]) == 0) {
					free(cssubin);
					break;
				}

				if (isNumeric(cssubin) == 1) {
					enumElement = atoi(cssubin);

					if (enumElement >= 0 && enumElement <= chemical_ecount) {
						traceElementInfo(enumElement, chemical_enames[enumElement], chemical_enames_eng[enumElement], chemical_heavy[enumElement], chemical_ionization_energy[enumElement], chemical_grstate_conf[enumElement], chemical_grstate_level[enumElement]);
					} else {
						printf(localization_table[46]);
					}
				} else {
					enumElement = atomExist(cssubin);

					if (enumElement != -1) {
						traceElementInfo(enumElement, chemical_enames[enumElement], chemical_enames_eng[enumElement], chemical_heavy[enumElement], chemical_ionization_energy[enumElement], chemical_grstate_conf[enumElement], chemical_grstate_level[enumElement]);
					} else {
						printf(localization_table[46]);
					}
				}

				free(cssubin);
			}

			goto csm_next;
		}

		/*  Balances the chemical equation  */
		ret = balanceCE(csin, *full);

		if (ret != NULL) {
			if (*silent == 0) {
				printf(localization_table[32]);
			}
			printf("%s\n", ret);
		} else {
			if (*silent == 0) {
				printf(localization_table[33]);
			} else {
				printf(localization_table[34]);
			}
		}

csm_next:
		free(csin);
	}
}

